package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AbJiuMapper;
import com.sinog.biz.mapper.havexml.AbPrisonerMapper;
import com.sinog.biz.mapper.havexml.GovernmentHelpMapper;
import com.sinog.biz.service.business.BjgzJiuzhuInfoMainService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.AbJiuResultVo;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.MapUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 安置帮教管理 - 帮教工作信息管理 - 救助管理
 * @author sunsf
 * @date 2020-02-07-16:40
 */
@Service
public class BjgzJiuzhuInfoMainServiceImpl extends ServiceImpl<AbJiuMapper,AbJiuResultVo> implements BjgzJiuzhuInfoMainService {

    @Resource
    private AbJiuMapper abJiuMapper;
    @Resource
    private GovernmentHelpMapper governmentHelpMapper;
    @Resource
    private AbPrisonerMapper abPrisonerMapper;

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 救助管理 - 救助 - 保存
     * @param dataStr dataStr
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveJzInfo(String dataStr,UserDTO userDto,String edit) {
        AbJiuResultVo abJiuResultVo = JsonUtil.toBean(dataStr,AbJiuResultVo.class);
        Date date = new Date();
        String id = abJiuResultVo.getId();
        //新增
        if(!(Constants.StrFinalValue.TRUEVALUE.equals(edit) && (StringUtils.isNotBlank(id)) && !Constants.NumberStr.NUM_STR_FU_ONE.equals(id))) {
            abJiuResultVo.setId(IdGen.strid());
            abJiuResultVo.setCreateUser(userDto.getUsername());
            abJiuResultVo.setCreateTime(date);
            abJiuResultVo.setAdduserid(userDto.getDepartid());
        }
        abJiuResultVo.setUpdateTime(date);
        abJiuResultVo.setUpdateUser(userDto.getUsername());
        boolean tag = abJiuResultVo.insertOrUpdate();
        if(tag) {
            //更新救助状态
            abPrisonerMapper.updateBjztExistsByPriNum(abJiuResultVo.getPriNum());
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 获取救助信息
     * @param jzId jzId
     * @return Result
     */
    @Override
    public Result getJzBaseInfo(String jzId) {
        Map<String,Object> map = abJiuMapper.getJzBaseInfoByPid(jzId);
        if(null != map.get(Constants.StrFinalValue.JZDW01) && !"".equals(map.get(Constants.StrFinalValue.JZDW01))) {
            map.put("xtdw01",governmentHelpMapper.selectById(map.get(Constants.StrFinalValue.JZDW01).toString())
                                                 .getXtdw());
        }
        if(null != map.get(Constants.StrFinalValue.JZDW02) && !"".equals(map.get(Constants.StrFinalValue.JZDW02))) {
            map.put("xtdw02",governmentHelpMapper.selectById(map.get(Constants.StrFinalValue.JZDW02).toString())
                                                 .getXtdw());
        }
        if(null != map.get(Constants.StrFinalValue.JZDW03) && !"".equals(map.get(Constants.StrFinalValue.JZDW03))) {
            map.put("xtdw03",governmentHelpMapper.selectById(map.get(Constants.StrFinalValue.JZDW03).toString())
                                                 .getXtdw());
        }
        if(null != map.get(Constants.StrFinalValue.JZDW04) && !"".equals(map.get(Constants.StrFinalValue.JZDW04))) {
            map.put("xtdw04",governmentHelpMapper.selectById(map.get(Constants.StrFinalValue.JZDW04).toString())
                                                 .getXtdw());
        }
        if(null != map.get(Constants.StrFinalValue.JZDW05) && !"".equals(map.get(Constants.StrFinalValue.JZDW05))) {
            map.put("xtdw05",governmentHelpMapper.selectById(map.get(Constants.StrFinalValue.JZDW05).toString())
                                                 .getXtdw());
        }
        if(null != map.get(Constants.StrFinalValue.JZDW06) && !"".equals(map.get(Constants.StrFinalValue.JZDW06))) {
            map.put("xtdw06",governmentHelpMapper.selectById(map.get(Constants.StrFinalValue.JZDW06).toString())
                                                 .getXtdw());
        }
        return Result.success(map);
    }

    /**
     * 获取基本信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getPrisonerBaseInfo(String priNum) {
        return Result.success(abPrisonerMapper.getPrisonerBaseInfo(priNum));
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 救助管理 - 救助 - 列表
     * @param priNum priNum
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getJzInfoList(String priNum) {
        //本系统的本地code前两位
        String flag = CodeUrlUtils.getLocalCodeStartTwo();
        List<Map<String,Object>> list = abJiuMapper.getJzInfoList(priNum,flag);
        List<Map<String,Object>> reList = new ArrayList<>(16);
        for(Map<String,Object> map : list) {
            reList.add(MapUtil.mapToString(map));
        }
        return reList;
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 救助管理 - 救助 - 删除
     * @param jzId jzId
     * @param priNum priNum
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delJzInfoAction(String jzId,String priNum) {
        int num = abJiuMapper.delJzInfoAction(jzId);
        if(0 < num) {
            //更新帮扶状态
            abPrisonerMapper.updateBjztByPriNum(priNum);
            return Result.success("删除成功");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }
}